﻿
var table, form, layer, rate, element, upload;
var active;

$(function () {
    layui.use(['table', 'form', 'rate', 'element', 'upload'], function () {
        table = layui.table
      , form = layui.form
      , layer = layui.layer
      , rate = layui.rate
      , element = layui.element
      , upload = layui.upload;

        InitUpload('btnYYZZ');
    });

    var img2xls = $('#img2xls');
    if (img2xls.val() == "1") {
        img2xls.parent().show();
    }

    $('#btnSubmit').click(function () {
        login();
    });

    document.onkeyup = function (e) {
        var code = e.charCode || e.keyCode;
        if (code == 13) {
            $('#btnSubmit').click();
        }
    }
})

var login = function () {
    var formdata = $('#loginForm').serializeObject();      //序列化
    var isCode = $('#isCode').val();
    if (isCode == "1") {
        var token = $('input[name="Token"]').val();
        if (token == "") {
            dialog.warning("请拖动滑块完成拼图！");
            return;
        }
    }
    $('#btnSubmit').prop('disabled', true);
    $.ajax({
        type: "post",
        url: Root + 'Login/Logon',
        data: { model: formdata },
        success: function (data) {
            if (data.SubmitResult) {
                location.href = Root + "Enterprise/index";
            } else {
                dialog.warning(data.Message);
                captcha.reload();
            }
        },
        error: function (result) {
            console.log(result);
            dialog.danger('操作失败！请重试或者联系管理员');
            captcha.reload();
        },
        complete: function () {
            $('input[name="Token"]').val('');
            $('#btnSubmit').prop('disabled', false);
        }
    });
}

var InitUpload = function (btn_uploadId) {
    var loading = null;
    upload.render({
        elem: '#' + btn_uploadId
        , url: Root + 'PublicUpload/UploadAnalysisImg2Excel' //改成您自己的上传接口
        , accept: 'file' //普通文件
        , before: function () {
            loading = layer.load(0, {
                shade: false,
                time: -1
            });
        }
        , done: function (res) {
            //console.log(res);
            if (res.SubmitResult) { //上传成功
                //将图片信息加载到文本中
                var obj = res.Object;
                $('#demoText').html('<a href="' + obj + '" target="_blank">下载 Excel</a>');
            }
            layer.close(loading);
        }, error: function () {
            layer.close(loading);
        }
    });
}